package com.itstudy.elasticsearch.service;

import com.alibaba.fastjson.JSON;
import com.itstudy.elasticsearch.pojo.Content;
import com.itstudy.elasticsearch.utils.ParseUtil;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * @ClassNameContentService
 * @Description TODO
 * @Author miaohuaixiang
 * @Date 2020/6/28 20:19
 * @Version 1.0
 **/
@Service
public class ContentService {
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    public  Boolean pareseContent(String keywords) throws  Exception{
        List<Content> contents = new ParseUtil().parseDj(keywords);
        BulkRequest bulkRequest=new BulkRequest();

        bulkRequest.timeout("2m");
        for (int i = 0; i <contents.size() ; i++) {
            System.out.println(JSON.toJSONString(contents.get(i)));
            bulkRequest.add(new IndexRequest("topic123").source(JSON.toJSONString(contents.get(i)),XContentType.JSON)
            );

        }
        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        return  !bulk.hasFailures();

    }



    public List<Map<String,Object>> serchPage(String  ketword, int pageNo, int pageSize) throws IOException {
        if(pageNo<=1){
            pageNo=1;
        }
        SearchRequest searchRequest=new SearchRequest("bigdata_p");
        SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
        sourceBuilder.from(pageNo);
        sourceBuilder.size(pageSize);
        //reference resources  /resources/elastic_search
        QueryBuilder queryBuilder = QueryBuilders.matchQuery("author", "Doug Cutting");
        sourceBuilder.query(queryBuilder);
       // MatchAllQueryBuilder termQueryBuilder = QueryBuilders.matchAllQuery();
      //  sourceBuilder.query(termQueryBuilder);
       // QueryBuilder queryBuilder = QueryBuilders.fuzzyQuery("message", ketword);
       // sourceBuilder.query(queryBuilder);

        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        ArrayList<Map<String,Object>> list = new ArrayList<>();
        for (SearchHit documentfiels:searchResponse.getHits().getHits()
             ) {
            list.add(documentfiels.getSourceAsMap());
        }

        return list;
    }
}



































